// component/bottomFrame/bottomFrame.js
const app = getApp()
import {
  list,
  find
} from '../../api/product'

import {
  add,
  buyNow
} from '../../api/cart'

const util = require('../../utils/util.js')

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    giftCardId: '',
    productId: "", //已选中的商品id
    showConfirm: "", //控制底部按键
    type: "", //判断是不是购物车页面的属性点击进来的
    isClose: "", //控制是关闭弹窗还是跳转订单确认页面
    flag: false,
    wrapAnimate: 'wrapAnimate',
    bgOpacity: 0,
    frameAnimate: 'frameAnimate',
    spec1Off: false, //第一属性的开关
    spec2Off: false, //第一属性的开关
    imageSrc: "",
    point:"",
    imageSrcOrl: "",
    price: "", //商品单价
    pricePeriod: "", //价格区间
    stock: "", //库存
    stockSum: 0, //总库存
    productState: true, //商品上下架状态
    minLimit: 1, //最小限购数量
    maxLimit: 0, //最大限购数量
    tagTextList: "", //显示所选的标签列表
    selectProduct: "", //被选中的商品
    currentNum: 1, //被选中的商品数量
    coupons: [], //优惠券
    promotions: [], //活动
    // spec1: "请选择配色分类", //第一属性
    // spec2: "请选择颜色", //第二属性
    //商品的属性列表
    attributeList: [
      //         {
      //                 name:"配色分类",
      //                 attrId:1,
      //                 tagList:[
      //                         {
      //                                 tagId:1,
      //                                 haveImage: true,
      //                                 src: "http://cdn.rzico.com/v4.0/footprint-redWine.jpg",
      //                                 tagname:"红色",
      //                                 isSelected:false,
      //                         },
      //                         {
      //                                 tagId: 2,
      //                                 haveImage: true,
      //                                 src: "http://cdn.rzico.com/v4.0/footprint-redWine.jpg",
      //                                 tagname: "红色",
      //                                 isSelected: true,
      //                         },
      //                         {
      //                                 tagId: 3,
      //                                 haveImage: true,
      //                                 src: "http://cdn.rzico.com/v4.0/footprint-redWine.jpg",
      //                                 tagname: "红色",
      //                                 isSelected: false,
      //                         },
      //                         {
      //                                 tagId: 4,
      //                                 haveImage: true,
      //                                 src: "http://cdn.rzico.com/v4.0/footprint-redWine.jpg",
      //                                 tagname: "红色",
      //                                 isSelected: false,
      //                         },

      //                 ]
      //         },
      //         {
      //                 name: "颜色分类",
      //                 attrId: 2,
      //                 tagList: [
      //                         {
      //                                 tagId: 1,
      //                                 haveImage: true,
      //                                 src: "http://cdn.rzico.com/v4.0/footprint-redWine.jpg",
      //                                 tagname: "3瓶5瓶5瓶5瓶5瓶",
      //                                 isSelected: false,
      //                         },
      //                         {
      //                                 tagId: 2,
      //                                 haveImage: true,
      //                                 src: "http://cdn.rzico.com/v4.0/footprint-redWine.jpg",
      //                                 tagname: "5瓶5瓶5瓶5瓶5瓶5瓶5瓶5瓶5瓶5瓶",
      //                                 isSelected: true,
      //                         },
      //                         {
      //                                 tagId: 3,
      //                                 haveImage: true,
      //                                 src: "http://cdn.rzico.com/v4.0/footprint-redWine.jpg",
      //                                 tagname: "10瓶",
      //                                 isSelected: false,
      //                         },
      //                         {
      //                                 tagId: 4,
      //                                 haveImage: true,
      //                                 src: "http://cdn.rzico.com/v4.0/footprint-redWine.jpg",
      //                                 tagname: "20瓶",
      //                                 isSelected: false,
      //                         },

      //                 ]
      // },
    ],
    //测试的服务端数据
    tagControlList: [], //测试属性的控制列表
    origionalTagControlList: [],
    response: [
      // {
      //         "id": 1,
      //         "marketPrice": 22,
      //         "name": "66",
      //         "spec1": "红色",
      //         "spec2": "36码",
      //         "price": 1,
      //         "stock": 10,
      //         "minLimit": 5
      // },
      // {
      //         "id": 2,
      //         "marketPrice": 22,
      //         "name": "66",
      //         "spec1": "红色",
      //         "spec2": "37码",
      //         "price": 2,
      //         "stock": 1,
      //         "minLimit": 2
      // },
      // {
      //         "id": 3,
      //         "marketPrice": 22,
      //         "name": "66",
      //         "spec1": "红色",
      //         "spec2": "38码",
      //         "price": 3,
      //         "stock": 0,
      //         "minLimit": 1
      // },
      // {
      //         "id": 4,
      //         "marketPrice": 22,
      //         "name": "66",
      //         "spec1": "白色",
      //         "spec2": "36码",
      //         "price": 4,
      //         "stock": 1
      // },
      // {
      //         "id": 5,
      //         "marketPrice": 22,
      //         "name": "66",
      //         "spec1": "白色",
      //         "spec2": "37码",
      //         "price": 5,
      //         "stock": 0
      // },
      // {
      //         "id": 6,
      //         "marketPrice": 22,
      //         "name": "66",
      //         "spec1": "白色",
      //         "spec2": "38码",
      //         "price": 6,
      //         "stock": 0
      // },
      // {
      //         "id": 7,
      //         "marketPrice": 22,
      //         "name": "66",
      //         "spec1": "黑色",
      //         "spec2": "36码",
      //         "price": 7,
      //         "stock": 0
      // },
      // {
      //         "id": 8,
      //         "marketPrice": 22,
      //         "name": "66",
      //         "spec1": "黑色",
      //         "spec2": "37码",
      //         "price": 8,
      //         "stock": 1
      // },
      // {
      //         "id": 9,
      //         "marketPrice": 22,
      //         "name": "66",
      //         "spec1": "黑色",
      //         "spec2": "38码",
      //         "price": 9,
      //         "stock": 1
      // },
      // {
      //         "id": 10,
      //         "marketPrice": 22,
      //         "name": "66",
      //         "spec1": "黑色",
      //         "spec2": "39码",
      //         "price": 9,
      //         "stock": 1,
      //         "minLimit": 2
      // },
      // {
      //         "id": 11,
      //         "marketPrice": 22,
      //         "name": "66",
      //         "spec1": "白色",
      //         "spec2": "39码",
      //         "price": 9,
      //         "stock": 1,
      //         "minLimit": 1
      // },
      // {
      //         "id": 12,
      //         "marketPrice": 22,
      //         "name": "66",
      //         "spec1": "红色",
      //         "spec2": "39码",
      //         "price": 9,
      //         "stock": 1,
      //         "minLimit": 4
      // }


    ],
  },

  attached: function() {
    // 组件生命周期函数，在组件实例进入页面节点树时执行。

    // this.formateResponse(this.data.response)
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //请求商品数据
    productInfoRequest(id) {
      console.log("商品id" + id)
      find(id).then(
        res => {
          console.log("单件商品数据--" + JSON.stringify(res))
          console.log(res)

          if (res.type == 'success') {

            if (!res.data.products[0].isMarketable) {
              this.setData({
                productState: false
              })

            }
            this.data.response = res.data.products
            this.formateResponse(res.data.products)
            this.setData({
              flag: true,
              wrapAnimate: 'wrapAnimate',
              frameAnimate: 'frameAnimate'
            })
            wx.hideLoading()
          }
        }
      ).catch(error => {
        console.log(error)
        wx.hideLoading()
        wx.showToast({
          title: '服务器开小差，请重试',
          icon: "none",
          duration: 1500
        })
      })
    },

    //显示底部弹窗
    showFrame(id, type, cardId) {

      if (!util.isNull(cardId)) {
        this.data.giftCardId = cardId

      }
      console.log("显示底部弹窗id--" + JSON.stringify(id))
      console.log("显示底部弹窗type--" + JSON.stringify(type))
      this.setData({
        productId: id
      });
      this.productInfoRequest(id)
      if (type == "buyNow") { //立即购买过来的
        //console.info("立即购买过来的")
        this.setData({
          showConfirm: true,
          isClose: false,

        });
      } else if (type == "addCart" || type == "changeAttr") { //点击加入购物车过来的
        //console.info("点击加入购物车过来的")
        this.setData({
          type: type,
          showConfirm: true,
          isClose: true,
        })
      } else if (type == "attribute") { //点击属性选择过来的
        //console.info("点击属性选择过来的")
        this.setData({
          type: "addCart",
          showConfirm: false,
        })
      }
      //console.info("showConfirm--" + this.data.showConfirm)
    },

    //隐藏底部弹窗
    hideFrame() {
      this.setData({
        wrapAnimate: 'wrapAnimateOut',
        frameAnimate: 'frameAnimateOut'
      });
      setTimeout(() => {
        this.setData({
          flag: false
        })
      }, 400)
    },

    //属性标签的选择事件
    selectAttr: function(e) {
      let attrname = e.currentTarget.dataset.attrname
      let tagname = e.currentTarget.dataset.tagname
      let canSelected = e.currentTarget.dataset.canselected
      let isSelected = e.currentTarget.dataset.isselected
      if (canSelected) {
        this.control(attrname, tagname, canSelected, isSelected)
      }
    },

    //控制函数
    control(attrname, tagname, canSelected, isSelected) {
      let tagControlList = this.data.tagControlList
      console.info("tagControlList" + JSON.stringify(tagControlList))
      let response = this.data.response
      
      console.info("response" + JSON.stringify(response))
      let zeroStockList = [] //零库存列表
      let haveStockList = [] //非零库存列表
      // if (canSelected) {
      if (response.length > 0) {
        response.map((item) => {
          if (tagname == item.spec1 || tagname == item.spec2) {
            //console.info("item" + JSON.stringify(item))
            if (item.stock > 0) {
              if (attrname == "规格") {
                haveStockList.push(item.spec2)
              } else {
                haveStockList.push(item.spec1)
              }
            }
          }
        })
      }
      if (tagControlList.length > 0) {
        if (attrname == "规格") {
          haveStockList.map((item) => {
            tagControlList.map((item1) => {
              if (item1.attrname == "颜色") {
                if (item1.tagname != item) {
                  zeroStockList.push(item1.tagname)
                }
              }
            })
          })
        } else {
          haveStockList.map((item) => {
            tagControlList.map((item1) => {
              if (item1.attrname == "规格") {
                if (item1.tagname != item) {
                  zeroStockList.push(item1.tagname)
                }
              }
            })
          })
        }
      }
      //console.info("zeroStockList--" + JSON.stringify(zeroStockList))
      //console.info("haveStockList--" + JSON.stringify(haveStockList))
      let origionalTagControlList = this.data.origionalTagControlList
      if (tagControlList.length > 0) {
        tagControlList.map((item) => {
          if (isSelected) {
            if (item.tagname == tagname) {
              item.isSelected = false
            }
            if (item.attrname != attrname) {
              if (origionalTagControlList.length > 0) {
                origionalTagControlList.map((origionalTagControlListItem) => {
                  if (item.tagname == origionalTagControlListItem.tagname) {
                    item.canSelected = origionalTagControlListItem.canSelected
                  }
                })
              }

            }
          } else {
            if (item.attrname == attrname) {
              item.isSelected = false
            } else {
              if (zeroStockList.length > 0) {
                zeroStockList.map((zeroStockListItem) => {
                  if (zeroStockListItem == item.tagname) {
                    item.canSelected = false
                  }
                })
              }
              if (haveStockList.length > 0) {
                haveStockList.map((haveStockListItem) => {
                  if (haveStockListItem == item.tagname) {
                    item.canSelected = true
                  }
                })
              }
            }
            if (item.tagname == tagname) {
              item.isSelected = true
            }

          }

        })
      }

      // }

      // let tagControlList = this.data.tagControlList
      let attributeList = this.data.attributeList
      console.info("attributeList--" + JSON.stringify(attributeList))
      let spec1 = ""
      let spec2 = ""

      if (tagControlList.length > 0) {
        tagControlList.map((item) => {
          if (attributeList.length > 0) {
            attributeList.map((item1) => {
              if (item.attrname == item1.attrname) {
                if (item1.tagList.length > 0) {
                  item1.tagList.map((item2) => {
                    if (item.tagname == item2.tagname) {
                      item2.isSelected = item.isSelected
                      item2.canSelected = item.canSelected
                    }
                  })
                }
              }
              if (item.isSelected == true) {
                if (item.attrname == "规格") {
                  spec1 = item.tagname
                } else {
                  spec2 = item.tagname
                }
              }
            })
          }
        })
      }
      // //console.info("spec1.length--" + spec1)
      // //console.info("spec2.length--" + spec2)
      let selectProduct = ""
      let tagTextList = this.data.tagTextList
      // if (!isSelected){
      if (response.length > 0) {
        if (attributeList.length == 2) {
          response.map((item) => {
            // //console.info("item--" + JSON.stringify(item))
            if (item.spec2 == spec2 && item.spec1 == spec1) {
              //console.info("我进来了")
              selectProduct = item
            }
          })
          if (spec1 == "" && spec2 == "") {
            tagTextList = "请选择： 规格，颜色"
            selectProduct = ""
          } else if (spec1 != "" && spec2 != "") {
            tagTextList = "已选：" + spec1 + "，" + spec2
          } else if (spec1 != "") {
            tagTextList = "请选择：颜色"
            selectProduct = ""
          } else {
            tagTextList = "请选择：规格"
            selectProduct = ""
          }
        } else if (attributeList.length == 1) {
          if (spec1 == "") {
            response.map((item) => {
              if (item.spec2 == spec2) {
                selectProduct = item
              }
            })
            if (spec2 == "") {
              tagTextList = "请选择：颜色"
              selectProduct = ""
            } else {
              tagTextList = "已选：" + spec2
            }
          } else if (spec2 == "") {
            response.map((item) => {
              if (item.spec1 == spec1) {
                selectProduct = item
              }
            })
            if (spec1 == "") {
              tagTextList = "请选择：规格"
              selectProduct = ""
            } else {
              tagTextList = "已选：" + spec1
            }
          }
        }
      }
      // }
      console.info("selectProduct--" + JSON.stringify(selectProduct))
      let price = this.data.price
      let stock = this.data.stock
      let minLimit = this.data.minLimit
      let maxLimit = this.data.maxLimit
      let imageSrc = this.data.imageSrc
      let coupons = this.data.coupons
      let promotions = this.data.promotions
      if (selectProduct != "") {
        price = selectProduct.price
        stock = selectProduct.stock
        minLimit = selectProduct.minLimit
        this.data.point=selectProduct.point
        imageSrc = util.thumbnail(selectProduct.thumbnail, 200, 200)
        coupons = selectProduct.coupons
        promotions = selectProduct.promotions
        if (selectProduct.maxLimit) {
          maxLimit = selectProduct.maxLimit
        }
        this.setData({
          point:this.data.point,
        })
      } else {
        price = this.data.pricePeriod
        stock = this.data.stockSum
        imageSrc = this.data.imageSrcOrl
      }
        console.log(this.data.point)
      this.setData({
        
        currentNum: minLimit > 0 ? minLimit : this.data.currentNum,
        imageSrc: imageSrc,
        selectProduct: selectProduct, //被选中的商品
        attributeList: attributeList,
        price: price, //商品单价
        stock: stock, //库存
        minLimit: minLimit, //限购数量
        tagTextList: tagTextList, //显示所选的标签列表
        coupons: coupons,
        promotions: promotions,
        maxLimit: maxLimit
      })
    },

    //服务器数据整理
    formateResponse(res) {
      console.info("服务器数据整理res" + JSON.stringify(res))
      let spec1List = []
      let spec2List = []
      let priceList = []
      let pricePeriod = this.data.pricePeriod
      let attributeList = []
      let stockSum = 0
      let spec1 = ""
      let spec2 = ""
      var imageSrcOrl = ""
      let productId = parseInt(this.data.productId)
      console.info("服务器数据整理productId--" + JSON.stringify(productId))
      let tagTextList = ""

      if (res[0].spec1 != "" || res[0].spec2 != "") {
        tagTextList = "请选择："
      }
      if (res.length > 0) {
        res.map((item) => {
          if (item.spec1) {
            spec1List.push(item.spec1)
            if (productId == item.id) {
              spec1 = item.spec1
            }
          }
          if (item.spec2) {
            spec2List.push(item.spec2)
            if (productId == item.id) {
              spec2 = item.spec2
            }
          }
          priceList.push(item.price)
          stockSum += item.stock
          item
        })
        imageSrcOrl = util.thumbnail(res[0].thumbnail, 200, 200)
      }

      //获取初始的价格区间
      priceList = Array.from(new Set(priceList))
      if (priceList.length > 1) {
        pricePeriod = Math.min.apply(Math, priceList) + "-" + Math.max.apply(Math, priceList)
      } else if (priceList.length > 0) {
        pricePeriod = priceList[0]
      }



      //去重
      if (spec1List.length > 0) {
        spec1List = Array.from(new Set(spec1List));
        tagTextList += "规格"
        let attributeListItem = {}
        attributeListItem.attrname = "规格"
        attributeListItem.tagList = []
        spec1List.map((item) => {
          let num = 0
          if (res.length > 0) {
            res.map((item1) => {
              if (item1.spec1 == item) {
                num += item1.stock
              }
            })
          }
          let tagListItem = {}
          if (num == 0) {
            tagListItem.canSelected = false
          } else if (num > 0) {
            tagListItem.canSelected = true
          }
          if (spec1 == item) {
            tagListItem.isSelected = true
          } else {
            tagListItem.isSelected = false
          }
          tagListItem.haveImage = false
          tagListItem.tagname = item
          attributeListItem.tagList.push(tagListItem)
        })
        attributeList.push(attributeListItem)
      }
      if (spec2List.length > 0) {
        spec2List = Array.from(new Set(spec2List));
        tagTextList += " 颜色"
        let attributeListItem = {}
        attributeListItem.attrname = "颜色"
        attributeListItem.tagList = []
        spec2List.map((item) => {
          let num = 0
          if (res.length > 0) {
            res.map((item1) => {
              if (item1.spec2 == item) {
                num += item1.stock
              }
            })
          }
          let tagListItem = {}
          if (num == 0) {
            tagListItem.canSelected = false
          } else if (num > 0) {
            tagListItem.canSelected = true
          }
          if (spec2 == item) {
            tagListItem.isSelected = true
          } else {
            tagListItem.isSelected = false
          }
          tagListItem.haveImage = false
          tagListItem.tagname = item
          attributeListItem.tagList.push(tagListItem)
        })
        attributeList.push(attributeListItem)
      }
      console.info("attributeList-- " + JSON.stringify(attributeList))
      let tagControlList = []
      let origionalTagControlList = []
      if (attributeList.length > 0) {
        attributeList.map((item) => {
          if (item.tagList.length > 0) {
            item.tagList.map((item1) => {
              let tagControlListItem = {}
              tagControlListItem.attrname = item.attrname
              tagControlListItem.haveImage = item1.haveImage
              tagControlListItem.isSelected = item1.isSelected
              tagControlListItem.canSelected = item1.canSelected
              tagControlListItem.tagname = item1.tagname
              tagControlList.push(tagControlListItem)
              let origionalTagControlListItem = {}
              origionalTagControlListItem.attrname = item.attrname
              origionalTagControlListItem.haveImage = item1.haveImage
              origionalTagControlListItem.isSelected = item1.isSelected
              origionalTagControlListItem.canSelected = item1.canSelected
              origionalTagControlListItem.tagname = item1.tagname
              origionalTagControlList.push(origionalTagControlListItem)
            })
          }
        })

      }

      console.info("tagControlList-- " + JSON.stringify(tagControlList))
      this.setData({
        origionalTagControlList: origionalTagControlList,
        tagControlList: tagControlList,
        attributeList: attributeList,
        pricePeriod: pricePeriod,
        price: pricePeriod,
        stockSum: stockSum,
        imageSrcOrl: imageSrcOrl,
        imageSrc: imageSrcOrl,
        stock: stockSum,
        tagTextList: tagTextList, //显示所选的标签列表
      })
      if (!util.isNull(spec1)) {
        console.info("我是规格的初始化器spec1--" + spec1)
        this.control("规格", spec1, true, false)
      }
      if (!util.isNull(spec2)) {
        console.info("我是颜色的初始化器spec2--" + spec2)
        this.control("颜色", spec2, true, false)
      }
      if (util.isNull(spec1) && util.isNull(spec2)) {
        let response = this.data.response
        console.info("response" + JSON.stringify(response))
        if (response.length > 0) {
          response.map(item => {
            if (item.id == productId) {
              this.data.selectProduct = item
              console.info("this.data.selectProduct" + JSON.stringify(this.data.selectProduct))
              if (this.data.selectProduct.minLimit > 0) {
                this.setData({
                  currentNum: this.data.selectProduct.minLimit,
                  minLimit: this.data.selectProduct.minLimit
                })
              }
              if (this.data.selectProduct.minLimit > 0) {
                this.setData({
                  maxLimit: this.data.selectProduct.maxLimit
                })
              }
            }
          })
        }

      }
    },

    //数量减少事件
    numReduce: function(e) {
      let selectProduct = this.data.selectProduct
      let currentNum = e.target.dataset.currentnum
      let minLimit = this.data.minLimit
      if (selectProduct) {
        let cartInfo = this.data.shopList
        console.info("currentNum--" + currentNum)
        if (currentNum <= minLimit || currentNum <= 1) {
          wx.showToast({
            title: '商品数量不能再少了呦！',
            icon: 'none',
            duration: 1500
          })
        } else {
          currentNum--
          //console.info("currentNum--" + currentNum)
          this.setData({
            currentNum: currentNum
          })
        }
      } else {
        wx.showToast({
          title: '请先选择商品属性呦！',
          icon: 'none',
          duration: 1500
        })
      }
    },

    //数量增加事件
    numAdd: function(e) {
      let selectProduct = this.data.selectProduct
      let currentNum = e.target.dataset.currentnum
      let maxLimit = 0
      console.info("selectProduct.maxLimit--" + selectProduct.maxLimit)
      if (selectProduct.maxLimit) {
        maxLimit = selectProduct.maxLimit
      }
      console.info("maxLimit--" + maxLimit)
      let stock = selectProduct.stock
      console.info("selectProduct--" + JSON.stringify(selectProduct))
      if (selectProduct) {
        console.info("maxLimit--" + maxLimit)
        if (maxLimit > 0) {
          if (currentNum >= maxLimit) {
            wx.showToast({
              title: '商品数量已达上限呦！',
              icon: 'none',
              duration: 1500
            })
          } else {
            currentNum++
            this.setData({
              currentNum: currentNum
            })
          }
        } else if (currentNum >= stock) {
          wx.showToast({
            title: '商品数量已达上限呦！',
            icon: 'none',
            duration: 1500
          })
        } else {
          console.info("我进来了--" + maxLimit)
          currentNum++
          this.setData({
            currentNum: currentNum
          })
        }
        // }
      } else {
        wx.showToast({
          title: '请先选择商品属性呦！',
          icon: 'none',
          duration: 1500
        })
      }
    },

    //数量输入框的失焦事件
    bindblur: function(e) {
      let selectProduct = this.data.selectProduct
      let originalNum = this.data.currentNum
      let currentNum = e.detail.value
      let minLimit = selectProduct == '' ? 0 : parseInt(selectProduct.minLimit)
      let maxLimit = selectProduct == '' ? 0 : parseInt(selectProduct.maxLimit)
      console.info("originalNum--" + originalNum)
      console.info("currentNum--" + currentNum)
      console.info("minLimit--" + minLimit)
      console.info("maxLimit--" + maxLimit)
      console.info("minLimit--" + typeof(minLimit))
      let stock = selectProduct == '' ? 0 : selectProduct.stock
      if (selectProduct) {
        if (currentNum < minLimit || currentNum < 1) {
          wx.showModal({
            title: '温馨提示',
            content: '商品数量不能小于下限呦！',
          })
          this.setData({
            currentNum: originalNum
          })
        } else if (maxLimit != 0) {
          if (currentNum > maxLimit) {
            wx.showModal({
              title: '温馨提示',
              content: '商品数量超过上限呦！',
            })
            this.setData({
              currentNum: originalNum
            })
          }
        } else if (currentNum > stock) {
          wx.showModal({
            title: '温馨提示',
            content: '商品数量超过上限呦！',
          })
          this.setData({
            currentNum: originalNum
          })
        } else {
          this.setData({
            currentNum: currentNum
          })
        }
      } else {
        wx.showToast({
          title: '请先选择商品属性呦！',
          icon: 'none',
          duration: 1500
        })
        this.setData({
          currentNum: originalNum
        })
      }
    },

    //检测用户的登陆状态
    checkLogin(callback) {
      console.info("全局变量app.globalData--" + JSON.stringify(app.globalData))
      if (app.globalData.token == null) {
        wx.navigateTo({
          url: '/pages/login/index',
        })
      } else {
        if (callback) {
          callback()
        }
      }
    },

    //确定的提交事件
    submit() {
      let selectProduct = this.data.selectProduct
      let currentNum = this.data.currentNum
      console.info("提交事件的selectProduct--" + JSON.stringify(selectProduct))
      this.checkLogin(() => {
        if (selectProduct) {
          if (this.data.stockSum > 0) {
            if (this.data.isClose) {
              this.addCartSubmit()
            } else {
              this.buySubmit()
            }
          }
        } else {
          wx.showToast({
            title: '请先选择商品属性呦！',
            icon: 'none',
            duration: 1500
          })
        }
      })
    },

    //加入购物车的按键事件
    addCartSubmit() {
      let _this = this
      this.checkLogin(() => {
        console.info("加入购物车的按键事件")
        let selectProduct = this.data.selectProduct
        // console.info("productId--" + JSON.stringify(selectProduct))
        let currentNum = this.data.currentNum
        if (selectProduct) {
          //调用加入购物车的接口，成功后关闭弹窗
          if (this.data.stockSum > 0) {
            console.info("this.data.type--" + this.data.type)

            if (this.data.type == "addCart") {
              //发送添加购物车的请求
              let data = {}
              data.productId = selectProduct.id
              data.promotionId = ""
              data.quantity = currentNum
              data.uuid = app.globalData.uuid
              console.info("加入购物车的请求data--" + JSON.stringify(data))
              add(data).then(
                res => {

                  if (res.type == 'success') {
                    let info = {}
                    info.productId = selectProduct.id
                    info.currentNum = currentNum
                    info.spec = selectProduct.spec1 ? selectProduct.spec1 : '' + " " + selectProduct.spec2 ? selectProduct.spec2 : '',
                      info.price = selectProduct.price
                    this.triggerEvent("selectedInfo", info)

                    this.hideFrame()
                    wx.showToast({
                      title: '成功加入购物车',
                      duration: 2000
                    })

                    app.event.emit("onCartChange");
                  }
                }
              ).catch(error => {
                console.log(error)
                this.hideFrame()
                wx.showToast({
                        title: error.content,
                  icon: 'none',
                  duration: 2000
                })
              })
            } else if (this.data.type == "changeAttr") {
              let info = {}
              info.productId = selectProduct.id
              info.currentNum = currentNum
              info.productInfo = selectProduct
              info.spec = selectProduct.spec1 ? selectProduct.spec1 : '' + " " + selectProduct.spec2 ? selectProduct.spec2 : '',
                this.triggerEvent("selectedInfo", info)
              this.hideFrame()
            }

          }
        } else {
          wx.showToast({
            title: '请先选择商品属性呦！',
            icon: 'none',
            duration: 1500
          })
        }
      })
    },

    //立即购买的按键事件
    buySubmit() {
      this.checkLogin(() => {
        //console.info("立即购买的按键事件")
        let selectProduct = this.data.selectProduct
        // console.info("selectProduct--" + JSON.stringify(selectProduct))
        let currentNum = this.data.currentNum
        if (selectProduct) {
          if (this.data.stockSum > 0) {
            //关闭弹窗跳转到订单确认的页面
            //发送添加购物车的请求
            let data = {}
            data.productId = selectProduct.id
            data.promotionId = ""
            data.quantity = currentNum
            data.uuid = app.globalData.uuid
            buyNow(data).then(
              res => {

                if (res.type == 'success') {
                  if (!util.isNull(this.data.giftCardId)) {
                    wx.navigateTo({
                      url: '/pages/goods/submit/index?id=' + this.data.giftCardId,
                      success: function(res) {},
                      fail: function(res) {},
                      complete: function(res) {},
                    })
                  } else {
                    wx.navigateTo({
                      url: '/pages/goods/submit/index',
                      success: function(res) {},
                      fail: function(res) {},
                      complete: function(res) {},
                    })
                  }

                  this.hideFrame()
                }
              }
            ).catch(error => {
              console.log(error)
              this.hideFrame()
              wx.showToast({
                      title: error.content,
                icon: 'none',
                duration: 2000
              })
            })
          }
        } else {
          wx.showToast({
            title: '请先选择商品属性呦！',
            icon: 'none',
            duration: 1500
          })
        }
      })
    },
  }
})